# 文件: bot_api_v1/app/services/helper/wechat_token_helper.py
# 首席研发团队最终审查版 (Final Reviewed Version)

from typing import Dict, Any
from fastapi import Depends, HTTPException, Header
from sqlalchemy.ext.asyncio import AsyncSession

from bot_api_v1.app.db.session import get_db
from bot_api_v1.app.core.logger import logger
from bot_api_v1.app.services.business.wechat_service import WechatService, WechatError

# 实例化服务
wechat_service = WechatService()

async def verify_token(
    token: str = Header(..., description="JWT Token, 格式为 'Bearer {token}'", alias="Authorization"),
    db: AsyncSession = Depends(get_db)
) -> Dict[str, Any]:
    
    # 【新增日志】打印收到的原始Header
    logger.debug(f"【验证阶段】收到的原始Authorization Header: '{token}'")
    if token.startswith("Bearer "):
        token = token[7:]
    
    # 【新增日志】打印剥离前缀后，即将用于验证的Token
    logger.debug(f"【验证阶段】即将用于验证的纯Token: '{token}'")
    try:
        # 调用服务层进行验证
        return await wechat_service.verify_token(token, db)
    except WechatError as e:
        raise HTTPException(
            status_code=401,
            detail=str(e)
        )